<template>
  <div class="echarts" ref="echarts" style="width:100%;height:100%"></div>
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: 'TodayECharts',
  data() {
    return {}
  },

  props: ['predictionList'],
  // 注册组件
  components: {},
  // 计算属性
  computed: {},
  // 生命周期函数
  mounted() {
    this.echartsInit()
  },
  // 侦听器
  watch: {
    predictionList: {
      // 自动触发,只能使用handler
      handler(newVal, oldVal) {
        // console.log(newVal);
        // console.log(oldVal);
        this.echartsInit()
      },
      // true表示侦听器立即触发(immediate默认为false)
      // immediate: true,
      // true表示深度侦听，侦听对象属性的变化
      deep: true
    }
  },
  // 事件函数
  methods: {
    echartsInit() {
      const chartDom = this.$refs.echarts
      const myChart = echarts.init(chartDom)

      // 日期
      let dateList = []
      // 实际值
      let powerList = []
      // 预测值
      let incomeList = []
      // 包边值
      let income = []

      // console.log('echartsInit111111', this.predictionList)
      if (this.predictionList) {
        this.predictionList.forEach(item => {
          dateList.push(item.time)
          powerList.push(item.reality)
          incomeList.push(item.value)
          income.push(item.max)
        })
      }

      let option = {
        title: {
          text: '预测对比'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          top: '20%',
          left: '2%',
          right: '2%',
          bottom: '5%',
          containLabel: true
        },
        legend: {
          data: ['实际值', '预测值', '包边值']
        },
        toolbox: {
          show: true,
          feature: {
            dataZoom: {
              yAxisIndex: 'none'
            },
            dataView: { readOnly: false },
            magicType: { type: ['line', 'bar'] },
            restore: {},
            saveAsImage: {}
          }
        },
        xAxis: {
          boundaryGap: true,
          data: dateList,
          axisLine: {
            lineStyle: {
              color: 'rgba(117, 168, 202, 0.4)'
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            color: '#000'
          }
        },
        yAxis: [
          {
            name: 'kw',
            type: 'value',
            inverse: false,
            splitLine: {
              show: true,
              lineStyle: {
                color: 'rgba(117, 168, 202, 0.3)',
                type: 'solid'
              }
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: '#0A5C95'
              }
            },
            axisLabel: {
              formatter: '{value}',
              textStyle: {
                color: '#000',
                fontSize: 12
              }
            },
            nameTextStyle: {
              color: '#000',
              padding: [0, 0, 0, 15],
              fontSize: 14
            },
            axisTick: {
              show: false
            }
          }
        ],

        series: [
          {
            name: '实际值',
            type: 'line',
            symbol: 'circle', //设定为实心点
            showAllSymbol: true,
            symbolSize: 0,
            smooth: true,

            data: powerList
          },
          {
            name: '预测值',
            type: 'line',
            symbol: 'circle', //设定为实心点
            showAllSymbol: true,
            symbolSize: 0,
            smooth: true,

            data: incomeList
          },
          {
            name: '包边值',
            type: 'line',
            symbol: 'circle', //设定为实心点
            showAllSymbol: true,
            symbolSize: 0,
            smooth: true,

            data: income
          }
        ]
      }

      option && myChart.setOption(option, true)
    }
  }
}
</script>
<style lang='scss' scoped>
</style>